//
//  CoinViewModel.swift
//  Wallet
//
//  Created by Julian on 2022/5/7.
//

import Foundation

struct CoinViewModel {
    // MARK: - DATA
    // 币名称
    let name: String
    // 币符号
    let symbol: String
    // 币额
    let amount: Double
    // 换算成当前币种金额
    var currencyAmount: Double {
        get {
            let currency = AppSettings.shared.currencyValue
            let rateMap = coin.rates?.rates.toDict { $0.fiatCurrency }
            let rateObj = rateMap?[currency.rawValue]?.first
            if let rate = Double(rateObj!.rate) {
                return coin.amount * rate
            } else { // 未想好处理方案
                return 0.0
            }
        }
    }
    
    private let coin: Coin
    
    // MARK: - INITIALIZE
    init(coin: Coin) {
        self.coin = coin
        self.name = coin.name
        self.symbol = coin.symbol
        self.amount = coin.amount
    }
}
